//
//  LCDetailViewController.m
//  TianXiaShi
//
//  Created by qianfeng01 on 15-7-12.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCDetailViewController.h"
#define kDetailUrl @"http://vcsp.ifeng.com/vcsp/appData/videoGuid.do?guid=%@"
#import <MediaPlayer/MediaPlayer.h>
#import "LCDetailModel.h"
#import "LCDetailCell.h"
#import "HotArray.h"
#import "RanklisArray.h"
#import "RelatedArray.h"
#import "LCHotRankListModel.h"
@interface LCDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *videoView;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *xianGuanButton;
@property (weak, nonatomic) IBOutlet UIButton *paiHangButton;
@property (weak, nonatomic) IBOutlet UIButton *reDianButton;



- (IBAction)renDian:(id)sender;
- (IBAction)paiHang:(id)sender;

- (IBAction)xiangGuan:(id)sender;
@property (nonatomic, strong) MPMoviePlayerController *player;
@property (nonatomic, strong) LCDetailModel *detailModel;
@property (nonatomic, assign) NSInteger page;
- (IBAction)back:(id)sender;
- (IBAction)collection:(id)sender;
- (IBAction)share:(id)sender;
@property (nonatomic) UITableView *tabView1;
@property (nonatomic) UITableView *tabView2;
@property (nonatomic) UITableView *tabView3;
@property (nonatomic) NSMutableArray *dataArray;


@end

@implementation LCDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page  =  0;
    [self downWebDataWithUrl:[NSString stringWithFormat:kDetailUrl,self.guid]];
    self.scrollView.userInteractionEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(kScreenSize.width*3, 0);
    self.xianGuanButton.selected = YES;
    [self.xianGuanButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [self.xianGuanButton setTintColor:[UIColor whiteColor]];
    [self.reDianButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [self.reDianButton setTintColor:[UIColor whiteColor]];
    [self.paiHangButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [self.paiHangButton setTintColor:[UIColor whiteColor]];
    [self creattabView];
}

- (void)creattabView
{
    self.tabView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-self.scrollView.frame.origin.y)];
    self.tabView2 = [[UITableView alloc]initWithFrame:CGRectMake(kScreenSize.width, 0, kScreenSize.width,kScreenSize.height-self.scrollView.frame.origin.y)];
    self.tabView3 = [[UITableView alloc]initWithFrame:CGRectMake(kScreenSize.width*2, 0, kScreenSize.width, kScreenSize.height-self.scrollView.frame.origin.y)];
    _tabView1.delegate = self;
    _tabView2.delegate = self;
    _tabView3.delegate = self;
    _tabView1.dataSource = self;
    _tabView2.dataSource = self;
    _tabView3.dataSource = self;
    [self.scrollView addSubview:_tabView1];
    [self.scrollView addSubview:_tabView2];
    [self.scrollView addSubview:_tabView3];
    [self.tabView1 registerNib:[UINib nibWithNibName:@"LCDetailCell" bundle:nil] forCellReuseIdentifier:@"LCDetailCell"];
    [self.tabView2 registerNib:[UINib nibWithNibName:@"LCDetailCell" bundle:nil] forCellReuseIdentifier:@"LCDetailCell"];
    [self.tabView3 registerNib:[UINib nibWithNibName:@"LCDetailCell" bundle:nil] forCellReuseIdentifier:@"LCDetailCell"];
    
}
- (void)initJosnModelWith:(NSDictionary *)dict
{
    self.detailModel = [[LCDetailModel alloc]init];
    [self.detailModel setValuesForKeysWithDictionary:dict];
    [self creatVideo];
    [self initData];
}
- (void)creatVideo
{
    NSDictionary *dict = self.detailModel.videoFiles[@"102"];
    self.player = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:dict[@"mediaUrl"]]];
    self.player.view.frame = self.videoView.frame;
    [self.videoView addSubview:[self.player view]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self.player play];
    
}
- (void)playBack:(NSNotification *)nf
{
    NSLog(@"播放完毕");
    LCSubMemItemModel*model = self.dataArray[1];
    LCDetailViewController *detail = [[LCDetailViewController alloc]init];
    detail.guid = model.guid;
    [self presentViewController:detail animated:YES completion:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
- (void)initData
{
    __weak typeof(self) weakSelf = self;
    [RelatedArray relateDataArrayWith:self.guid Block:^(NSMutableArray *array) {
        weakSelf.dataArray =array;
        [weakSelf.tabView1 reloadData];
    }];
    HotArray *hot = [HotArray sharedHot];
    self.hotList = [hot getModel];
    RanklisArray *rank = [RanklisArray sharedRank];
    self.rankList = [rank getModel];
   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.tabView1) {
        return self.dataArray.count;
    }
    else if (tableView==self.tabView2) {
        return self.hotList.bodyList.count;
    }
    else if (tableView==self.tabView3) {
        return self.rankList.bodyList.count;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LCDetailCell" forIndexPath:indexPath];
    if (tableView==self.tabView1) {
        LCSubMemItemModel*model = self.dataArray[indexPath.row];
        [cell showDetilLModel:model];
        return cell;
    }
    else if (tableView==self.tabView2) {
        LCVideoModel *videoModel = self.hotList.bodyList[indexPath.row];
        [cell showVideoModel:videoModel];
        return cell;
    }
    else if (tableView==self.tabView3) {
        LCVideoModel *videoModel = self.rankList.bodyList[indexPath.row];
        [cell showVideoModel:videoModel andindexPath:indexPath];
        return cell;

    }
    return nil;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
     NSLog(@"%f",scrollView.contentOffset.x);
    int i = scrollView.contentOffset.x/kScreenSize.width;
    if (i==self.page) {
        return;
    }
    else
    {
        self.page=i;
    }
    if (i==0) {
        self.reDianButton.selected = NO;
        self.xianGuanButton.selected =  YES;
        self.paiHangButton.selected = NO;
    }
    else if (i==1)
    {
        self.reDianButton.selected = YES;
        self.xianGuanButton.selected =  NO;
        self.paiHangButton.selected = NO;
    }
    else
    {
        self.reDianButton.selected = NO;
        self.xianGuanButton.selected =  NO;
        self.paiHangButton.selected = YES;
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.tabView1) {
         LCSubMemItemModel*model = self.dataArray[indexPath.row];
        LCDetailViewController *detail = [[LCDetailViewController alloc]init];
        detail.guid = model.guid;

        detail.playBlock = ^
        {
            [self.player play];
        };
        [self presentViewController:detail animated:YES completion:nil];
    }
    else if (tableView==self.tabView2) {
        LCVideoModel *videoModel = self.hotList.bodyList[indexPath.row];
        
        LCDetailViewController *detail = [[LCDetailViewController alloc]init];
        detail.guid = videoModel.memberItem.guid;
        detail.playBlock = ^
        {
            [self.player play];
        };
        
        [self presentViewController:detail animated:YES completion:nil];
    }
    else if (tableView==self.tabView3) {
        LCVideoModel *videoModel = self.rankList.bodyList[indexPath.row];
        
        LCDetailViewController *detail = [[LCDetailViewController alloc]init];
        detail.guid = videoModel.memberItem.guid;
        detail.playBlock = ^
        {
            [self.player play];
        };

        [self presentViewController:detail animated:YES completion:nil];
    }
}

- (IBAction)renDian:(id)sender {
    self.scrollView.contentOffset = CGPointMake(kScreenSize.width, 0);
    self.reDianButton.selected = YES;
    self.xianGuanButton.selected =  NO;
    self.paiHangButton.selected = NO;
}

- (IBAction)paiHang:(id)sender {
    self.scrollView.contentOffset = CGPointMake(kScreenSize.width*2, 0);
    self.reDianButton.selected = NO;
    self.xianGuanButton.selected =  NO;
    self.paiHangButton.selected = YES;
    
}

- (IBAction)xiangGuan:(id)sender {
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.reDianButton.selected = NO;
    self.xianGuanButton.selected =  YES;
    self.paiHangButton.selected = NO;
}
- (IBAction)back:(id)sender {
    //收藏为历史足迹
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.playBlock) {
            self.playBlock();
        }
        
    }];
    
}

- (IBAction)collection:(id)sender {
    
}

- (IBAction)share:(id)sender {
}
@end
